package com.qingcheng.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.SeckillOrderMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.seckill.SeckillOrder;
import com.qingcheng.service.seckill.SeckillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class SeckillOrderServiceImpl implements SeckillOrderService {

    @Autowired
    private SeckillOrderMapper seckillOrderMapper;

    /**
     * 返回全部记录
     * @return
     */
    public List<SeckillOrder> findAll() {
        return seckillOrderMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<SeckillOrder> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<SeckillOrder> seckillOrders = (Page<SeckillOrder>) seckillOrderMapper.selectAll();
        return new PageResult<SeckillOrder>(seckillOrders.getTotal(),seckillOrders.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<SeckillOrder> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return seckillOrderMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<SeckillOrder> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<SeckillOrder> seckillOrders = (Page<SeckillOrder>) seckillOrderMapper.selectByExample(example);
        return new PageResult<SeckillOrder>(seckillOrders.getTotal(),seckillOrders.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public SeckillOrder findById(Long id) {
        return seckillOrderMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param seckillOrder
     */
    public void add(SeckillOrder seckillOrder) {
        seckillOrderMapper.insert(seckillOrder);
    }

    /**
     * 修改
     * @param seckillOrder
     */
    public void update(SeckillOrder seckillOrder) {
        seckillOrderMapper.updateByPrimaryKeySelective(seckillOrder);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(Long id) {
        seckillOrderMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(SeckillOrder.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 用户
            if(searchMap.get("userId")!=null && !"".equals(searchMap.get("userId"))){
                criteria.andLike("userId","%"+searchMap.get("userId")+"%");
            }
            // 商家
            if(searchMap.get("sellerId")!=null && !"".equals(searchMap.get("sellerId"))){
                criteria.andLike("sellerId","%"+searchMap.get("sellerId")+"%");
            }
            // 状态，0未支付，1已支付
            if(searchMap.get("status")!=null && !"".equals(searchMap.get("status"))){
                criteria.andLike("status","%"+searchMap.get("status")+"%");
            }
            // 收货人地址
            if(searchMap.get("receiverAddress")!=null && !"".equals(searchMap.get("receiverAddress"))){
                criteria.andLike("receiverAddress","%"+searchMap.get("receiverAddress")+"%");
            }
            // 收货人电话
            if(searchMap.get("receiverMobile")!=null && !"".equals(searchMap.get("receiverMobile"))){
                criteria.andLike("receiverMobile","%"+searchMap.get("receiverMobile")+"%");
            }
            // 收货人
            if(searchMap.get("receiver")!=null && !"".equals(searchMap.get("receiver"))){
                criteria.andLike("receiver","%"+searchMap.get("receiver")+"%");
            }
            // 交易流水
            if(searchMap.get("transactionId")!=null && !"".equals(searchMap.get("transactionId"))){
                criteria.andLike("transactionId","%"+searchMap.get("transactionId")+"%");
            }


        }
        return example;
    }

}
